package top.dingwen.io.treasure.sensitive.autoconfigure;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import top.dingwen.io.treasure.sensitive.constant.SensitiveConstant;
import top.dingwen.io.treasure.sensitive.core.WordsContext;
import top.dingwen.io.treasure.sensitive.core.WordsFilter;
import top.dingwen.io.treasure.sensitive.service.ISensitiveWordsService;
import top.dingwen.io.treasure.sensitive.service.impl.SensitiveWordsServiceImpl;
import top.dingwen.io.treasure.sensitive.web.SensitiveIndexController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * SensitiveAutoconfiguration : 自动配置类
 *
 * @author dingwen
 * @since 2024/10/16 13:28
 */
@MapperScan("top.dingwen.io.treasure.sensitive.mapper")
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(SensitiveProperties.class)
@Lazy(value = false)
@Slf4j
public class SensitiveAutoconfiguration {

    @Resource
    private SensitiveProperties sensitiveProperties;

    /**
     * 初始化
     */
    @PostConstruct
    public void init() {
        if (log.isInfoEnabled()) {
            log.info("{}\n\n                     _ _   _           \n" +
                            "                    (_) | (_)          \n" +
                            "  ___  ___ _ __  ___ _| |_ ___   _____ \n" +
                            " / __|/ _ / '_ // __| | __| /  / / _ \\n" +
                            "/__ /  __/ | | /__ / | |_| // V /  __/\n" +
                            " |___/__|_| |_|___/_/__|_| /_/ /___|\n" +
                            "                                       \n"
                    , SensitiveConstant.SEN_L_P_S);
            log.info("{},读取到的配置如下:\n{}", SensitiveConstant.SEN_L_P_S, JSONUtil.toJsonPrettyStr(sensitiveProperties));
            log.info("{}，启动成功", SensitiveConstant.SEN_L_P_S);
        }
    }

    /**
     * 默认API
     *
     * @return {@link SensitiveIndexController }
     */
    @ConditionalOnProperty(name = "dingwen.treasure.sensitive.defaultApiEnabled", havingValue = "true")
    @Bean
    @ConditionalOnMissingBean(SensitiveIndexController.class)
    public SensitiveIndexController sensitiveIndexController() {
        log.info("{},{},已经成功加载", SensitiveConstant.SEN_L_P_S, SensitiveConstant.SEN_L_P_API);
        return new SensitiveIndexController();
    }

    /**
     * 敏感词服务
     *
     * @return {@link ISensitiveWordsService } 服务
     */
    @Bean
    @ConditionalOnMissingBean(ISensitiveWordsService.class)
    public ISensitiveWordsService sensitiveWordsService() {
        log.info("{},{},已经成功加载", SensitiveConstant.SEN_L_P_S, SensitiveConstant.SEN_L_P_SWS);
        return new SensitiveWordsServiceImpl();
    }

    /**
     * 敏感词上下文
     *
     * @return {@link WordsContext }
     */
    @Bean
    @ConditionalOnMissingBean(WordsContext.class)
    public WordsContext wordsContext() {
        log.info("{},{},已经成功加载", SensitiveConstant.SEN_L_P_S, SensitiveConstant.SEN_L_P_WC);
        return new WordsContext();
    }

    /**
     * 敏感词过滤器
     *
     * @return {@link WordsFilter }
     */
    @Bean
    @ConditionalOnMissingBean(WordsFilter.class)
    public WordsFilter wordsFilter() {
        log.info("{},{},已经成功加载", SensitiveConstant.SEN_L_P_S, SensitiveConstant.SEN_L_P_WF);
        return new WordsFilter();
    }
}
